<?php
    namespace Home\Controller;
    use Think\Controller;
    class JixiaozhibiaoController extends Controller {

    	public function _initialize()
	    {
	        header("Access-Control-Allow-Origin: *");  

	        header("Access-Control-Allow-Credentials: true"); 
	      
	        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
	        
	        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

	    }

	    public function get_list()
	    {
	    	$type = "奖金";
	    	$page = I("get.page");
        	$limit = I("get.limit");
        	$guide_type = I("get.guide_type");
        	$account_date = strtotime(I("get.account_date"));

        	$list = M("performance.set_bonusguide_gather")->where("guide_type = '$guide_type' and account_date = $account_date")->select();

	    	$data = M("hospitalsys.guide_name_dict")->where(" guide_type = '$type'")->limit(($page-1)*$limit,$limit)->select();

	    	

	    	for ($i=0; $i < count($data); $i++) 
	    	{ 
	    		for ($j=0; $j < count($list); $j++) 
	    		{ 
	    			if($list[$j]["guide_code"] == $data[$i]["guide_code"] )
	    			{
	    				$data[$i]['sort'] = $list[$j]['sort'];
	    			}
	    		}
	    	}

	    	foreach ($data as $info) {
			    $sort[] = $info['sort'];
			}

			array_multisort($sort, SORT_ASC, $data);

			$arr = [];


	    	for ($i=0; $i < count($data); $i++) 
	    	{
	    		if(!empty($data[$i]['sort']))
	    		{
	    			$arr[] = $data[$i];
	    		}
	    	}
	    	for ($i=0; $i < count($data); $i++) 
	    	{
	    		if(empty($data[$i]['sort']))
	    		{
	    			$data[$i]['sort'] = 0;
	    			$arr[] = $data[$i];
	    		}
	    	}

	    	$json["data"] = $arr;
	        $json["code"] = 0;
	        $json["count"] = M("hospitalsys.guide_name_dict")->where(" guide_type = '$type'")->count();
	        $json["msg"] = "";
	        
	        $this->ajaxReturn($json,"json");
	    }

	    public function get_account_type()
	    {
	    	$data = M("performance.set_accounttype")->select();
	        
	        $json["data"] = $data;
	        $json["code"] = 0;
	        $json["count"] = M("performance.set_accounttype")->count();
	        $json["msg"] = "";
	        
	        $this->ajaxReturn($json,"json");
	    }

	    public function save_bonus_guide()
	    {
	    	$data = I('post.');
	    	$data['account_date'] = strtotime($data['account_date']);
	    	$sort = $data['sort'];
	    	unset($data['sort']);
	    	M("performance.set_bonusguide_gather")->where($data)->delete();
	    	$data['sort'] = $sort;
	    	$result = M("performance.set_bonusguide_gather")->add($data);
	    	if($result !== false)
	        {
	            $json["code"] = 0;
	            $json["msg"] = "修改成功";
	        }
	        else
	        {
	            $json["code"] = -1;
	            $json["msg"] = "修改失败";
	        }
	        $this->ajaxReturn($json,"json");
	    }


    }
